// pages/paymoney/paymoney.js
const app = getApp()
Page({
  data: {
    carlistdata: '',
    amount: '',
    name: '',
    cur: '',
    carlist: '',
    chooseadress: '',
    adress: '',
    showPop: false,
    currentData: 0,
    total: '',
    datainfo: '',
    srdzlist: '',
    numval: '',
    xgimg: '',
    pid: '',

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    this.getadress();
    if (options.type == 'srdz') { //从私人订制跳转
      let json = JSON.parse(options.josn);
      let datainfo = JSON.parse(options.datainfo);
      this.setData({
        srdzlist: json,
        datainfo: datainfo,
        numval: options.numval,
        xgimg: options.xgimg,
        pid: options.pid
      })
      this.getsrdz();
    } else if (options.type == 'bycar') { //从购物车跳转
      this.getlistfromcar();
    }
  },


  showmodel() { //打开弹窗
    this.setData({
      showPop: true
    })
    // this.getadress();

  },
  trigDetailBox1: function() { //关闭弹窗
    this.setData({
      showPop: false
    })
  },
  getadress() { //获取地址
    let _that = this;
    wx.request({ //传后台
      url: app.globalData.url + '/jxb/oneself/getAddressList',
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        token: wx.getStorageSync('token'),
        uid: wx.getStorageSync('uid')
      },
      success: function(res) {
        if(res.data.data){
          for (var i = 0; i < res.data.data.length; i++) {
            if (res.data.data[i].status == 1) {
              _that.setData({
                chooseadress: res.data.data[i],
              });
            }
          }
          _that.setData({
            adress: res.data.data,
          });
        }
      
      },

    })
  },
  radioval: function(e) { //单选按钮
    let item = e.currentTarget.dataset.item;
    let index = e.currentTarget.dataset.index;
    let adress = this.data.adress;
    for (var i = 0; i < adress.length; i++) {
      adress[i].status = 0;
    }
    adress[index].status = 1;
    this.setData({
      chooseadress: item,
      adress: adress,
      showPop: false
    })
  },
  addadress: function() { //跳转添加地址
    this.setData({
      showPop: false
    })
    wx.navigateTo({
      url: '/pages/adress/addadress/addadress?type=pay'
    })
  },
  getlistfromcar() { //从购物车下单
    let _that = this;
    wx.request({ //传后台
      url: app.globalData.url + '/jxb/order/getAddressOrderCartList',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        token: wx.getStorageSync('token'),
        uid: wx.getStorageSync('uid')
      },
      success: function(res) {
        console.log(res.data.data)
        _that.setData({
          carlistdata: res.data.data,
          carlist: res.data.data.itemlist,
          total: res.data.data.total,
          cur: res.data.data.cur,
          name: res.data.data.name,
          amount: res.data.data.amount
        });
      },

    })
  },
  getsrdz() { //从私人定制下单
    let _that = this;
    let datainfo = this.data.datainfo;
    let srdzlist = this.data.srdzlist[0];
    console.log(datainfo)
    wx.request({ //传后台
      url: app.globalData.url + '/jxb/order/getAddressOrderMade',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        token: wx.getStorageSync('token'),
        uid: wx.getStorageSync('uid'),
        pid: this.data.pid,
        gid: srdzlist.gid,
        pidiurl: this.data.xgimg,
        loan: datainfo.name,
        quotation: datainfo.talk,
        position: datainfo.location,
        num: this.data.numval,
        tcolor: datainfo.color == 'fff' ? '0' : '1'
      },
      success: function(res) {
        console.log(res.data.data)
        _that.setData({
          carlistdata: res.data.data,
          carlist: res.data.data.itemlist,
          total: res.data.data.total,
          cur: res.data.data.cur,
          // name: res.data.data.name,
          amount: res.data.data.amount
        });
      },

    })
  },

  paymoney(oid) { // 支付
    let that = this;
    wx.request({ //传后台
      url: app.globalData.url + '/jxb/pay/doOrder',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        oid: oid,
        token: wx.getStorageSync('token'),
        uid: wx.getStorageSync('uid'),
      },
      success: function(res) {
        console.log(res.data)
        that.doWxPay(res.data);
      },

    })
  },
  cratedorder() { //支付
    let that = this;
    wx.request({ //传后台
      url: app.globalData.url + '/jxb/order/createOrder',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        addid: this.data.chooseadress.id,
        token: wx.getStorageSync('token'),
        uid: wx.getStorageSync('uid'),
        info: JSON.stringify(this.data.carlistdata)
      },
      success: function(res) {
        let oid = res.data.data.oid
        if (oid) {
          that.paymoney(oid)
        }

      },

    })
  },
  seeview(e) {//预览图片
    let pidiurl = e.currentTarget.dataset.pidiurl
    wx.previewImage({
      urls: [pidiurl],
    })
  },
  doWxPay(param) { //微信支付
    let that = this;
    wx.requestPayment({
      timeStamp: param.timeStamp, //记住，这边的timeStamp一定要是字符串类型的，不然会报错
      nonceStr: param.nonceStr,
      package: param.package,
      signType: 'MD5',
      paySign: param.paySign,
      success: function(event) {
        wx.showToast({
          title: '支付成功',
          icon: 'success',
          duration: 2000

        });
        that.paysucess(); //支付成功
      },

      fail: function(error) {
        wx.showToast({
          title: '已取消',
          icon: 'none',
          duration: 2000,
        });
        wx.navigateBack()
      },
      complete: function() {
        console.log("pay complete")
      }

    });

  },
  pay() { //支付
    if (this.data.chooseadress == '') {
      wx.showModal({
        title: '稍等一下',
        confirmText: '好的',
        confirmColor: '#28A8F9',
        showCancel: false,
        content: '亲，请添加地址信息',
        success: function(res) {
          if (res.confirm) {} else if (res.cancel) {
            return;
          }
        }
      })
    } else {
      this.cratedorder()
    }
  },
  paysucess() { //支付成功跳转
    let that = this;
    wx.request({ //传后台
      url: app.globalData.url + '/jxb/order/isComment',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        token: wx.getStorageSync('token'),
        uid: wx.getStorageSync('uid'),
      },
      success: function(res) {
        let pstatus = res.data.data.pstatus;
        console.log(pstatus)
        if (pstatus == '1') { //已评价
          wx.navigateTo({
            url: '/pages/paymore/paymore',
          })
        } else {
          wx.navigateTo({
            url: '/pages/ratestart/ratestart',
          })
        }
      },

    })
  }

})